package models

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"house/appconfig"
)

type House struct {
	ID       int     `gorm:"int" json:"id"`
	Address  string  `gorm:"type:varchar(30)" json:"address"`
	Name     string  `gorm:"type:varchar(30)" json:"name"`
	Men      int     `gorm:"type:int" json:"men"`
	HouseID  int     `gorm:"type:int" json:"house_id"`
	Status   string  `gorm:"type:varchar(30)" json:"status"`
	Standard string  `gorm:"type:varchar(50)" json:"standard"`
	Area     float64 `gorm:"type:decimal(10,2)" json:"area"`
	Rent     int     `gorm:"type:int" json:"rent"`
}

var DB *gorm.DB

func init() {
	var err error
	q := appconfig.AppConf.MysqlConfig
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", q.User, q.Password, q.Host, q.Port, q.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("数据库链接失败")
	} else {
		fmt.Println("数据库链接成功")
	}
	DB.AutoMigrate(&House{})
}
